import javax.swing.event.*;

import com.imooc.ireader.utils.TTS;
import org.apache.commons.io.FileUtils;

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import javax.swing.*;
/*
 * Created by JFormDesigner on Thu Jan 27 13:44:42 CST 2022
 */



/**
 * @author Gaoshi_nol
 * @description 慕课网教程，小慕听书项目
 */
public class MainForm extends JPanel {
    private int speed = 40;
    private int volume = 80;

    public MainForm() {
        initComponents();
        // 设置滑块的位置
        this.sldSpeed.setValue(this.speed);
        this.sldVolume.setValue(this.volume);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        // 绘制背景图片
        ImageIcon icon = new ImageIcon(this.getClass().getResource("/images/bg.png"));
        g.drawImage(icon.getImage(), 0, 0, this.getWidth(), this.getHeight(), this);
    }

    public static void main(String[] args) {
        JFrame jFrame = new JFrame();
        jFrame.setSize(900, 790);
        jFrame.setContentPane(new MainForm());
        jFrame.setTitle("小慕听书");
        jFrame.setResizable(false);
        jFrame.setVisible(true);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private void doTTS(String voiceName) {
        String text = this.areaText.getText();
        if (text.trim().length() == 0) {
            JOptionPane.showMessageDialog(null, "内容不能为空", "警告", JOptionPane.WARNING_MESSAGE);
            return;
        } else if (text.trim().length() > 1000) {
            JOptionPane.showMessageDialog(null, "所选文件内容字符不能超过1000", "警告", JOptionPane.WARNING_MESSAGE);
            return;
        }

        TTS tts = new TTS();
        tts.appid = "58ae381b";
        tts.apiSecret = "f5854f23bffe5321bc6af4b706548883";
        tts.apiKey = "5f717143bb5a30badbd1d14e008aa429";

//        tts.appid = "5fe8938a";
//        tts.apiSecret = "2bb40a9d747f18ce265af43692953a61";
//        tts.apiKey = "8659ffe01e9d5ab5d1a13f0ca2420896";
        tts.voiceLocation = "./voice_iReader";
        String authUrl = tts.getAuthUrl();
        String filePath = tts.startDoTTS(authUrl, text, voiceName, this.speed, this.volume);
        tts.play(filePath);
    }

    private void btnMaleMouseClicked(MouseEvent e) {
        // TODO add your code here
        doTTS("aisjiuxu");
    }

    private void sldSpeedStateChanged(ChangeEvent e) {
        // TODO add your code here
        this.speed = sldSpeed.getValue();
    }

    private void sldVolumeStateChanged(ChangeEvent e) {
        // TODO add your code here
        this.volume = sldVolume.getValue();
    }

    private void btnFemaleMouseClicked(MouseEvent e) {
        // TODO add your code here
        doTTS("xiaoyan");
    }

    private void btnDialectMouseClicked(MouseEvent e) {
        // TODO add your code here
        doTTS("xiaokun");
    }

    private void btnChooserMouseClicked(MouseEvent e) {
        // TODO add your code here
        JFileChooser chooser = new JFileChooser();
        chooser.showOpenDialog(chooser);
        File readFile = chooser.getSelectedFile();
        // 判断文件是否为空，应对没有选择文件的情况
        if (readFile == null) {
            return;
        }
        // 判断文件是否存在，应对手动输入的文件不存在的情况
        if (!readFile.exists()) {
            JOptionPane.showMessageDialog(null, "文件不存在！", "警告", JOptionPane.WARNING_MESSAGE);
            return;
        }
        this.txtFile.setText(readFile.getName());
        try {
            this.areaText.setText(FileUtils.readFileToString(readFile, "UTF-8"));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        label1 = new JLabel();
        txtFile = new JTextField();
        btnChooser = new JButton();
        scrollPane1 = new JScrollPane();
        areaText = new JTextArea();
        label2 = new JLabel();
        label3 = new JLabel();
        sldSpeed = new JSlider();
        sldVolume = new JSlider();
        btnMale = new JButton();
        btnFemale = new JButton();
        btnDialect = new JButton();

        //======== this ========
        setMaximumSize(new Dimension(900, 765));
        setBackground(new Color(153, 153, 153));
        setLayout(null);

        //---- label1 ----
        label1.setText("\u6587\u4ef6");
        label1.setFont(new Font("\u9ed1\u4f53", Font.PLAIN, 20));
        label1.setForeground(Color.white);
        add(label1);
        label1.setBounds(55, 40, 44, 24);

        //---- txtFile ----
        txtFile.setEditable(false);
        txtFile.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 20));
        add(txtFile);
        txtFile.setBounds(110, 35, 605, 35);

        //---- btnChooser ----
        btnChooser.setText("\u9009\u62e9");
        btnChooser.setFont(new Font("\u9ed1\u4f53", Font.PLAIN, 20));
        btnChooser.setForeground(Color.white);
        btnChooser.setBackground(new Color(8, 145, 224));
        btnChooser.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                btnChooserMouseClicked(e);
            }
        });
        add(btnChooser);
        btnChooser.setBounds(730, 35, 105, 35);

        //======== scrollPane1 ========
        {

            //---- areaText ----
            areaText.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", Font.PLAIN, 20));
            areaText.setLineWrap(true);
            scrollPane1.setViewportView(areaText);
        }
        add(scrollPane1);
        scrollPane1.setBounds(25, 85, 845, 370);

        //---- label2 ----
        label2.setText("\u8bed\u901f");
        label2.setFont(new Font("\u9ed1\u4f53", Font.PLAIN, 20));
        label2.setForeground(Color.white);
        add(label2);
        label2.setBounds(140, 470, 44, 24);

        //---- label3 ----
        label3.setText("\u97f3\u91cf");
        label3.setFont(new Font("\u9ed1\u4f53", Font.PLAIN, 20));
        label3.setForeground(Color.white);
        add(label3);
        label3.setBounds(140, 505, 44, 24);

        //---- sldSpeed ----
        sldSpeed.setOpaque(false);
        sldSpeed.addChangeListener(e -> sldSpeedStateChanged(e));
        add(sldSpeed);
        sldSpeed.setBounds(220, 470, 530, sldSpeed.getPreferredSize().height);

        //---- sldVolume ----
        sldVolume.setOpaque(false);
        sldVolume.addChangeListener(e -> sldVolumeStateChanged(e));
        add(sldVolume);
        sldVolume.setBounds(220, 505, 530, sldVolume.getPreferredSize().height);

        //---- btnMale ----
        btnMale.setIcon(new ImageIcon(getClass().getResource("/images/btn_male.png")));
        btnMale.setOpaque(false);
        btnMale.setBorderPainted(false);
        btnMale.setBackground(new Color(153, 153, 153));
        btnMale.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                btnMaleMouseClicked(e);
            }
        });
        add(btnMale);
        btnMale.setBounds(254, 570, 100, 100);

        //---- btnFemale ----
        btnFemale.setIcon(new ImageIcon(getClass().getResource("/images/btn_female.png")));
        btnFemale.setOpaque(false);
        btnFemale.setBorderPainted(false);
        btnFemale.setBackground(new Color(153, 153, 153));
        btnFemale.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                btnFemaleMouseClicked(e);
            }
        });
        add(btnFemale);
        btnFemale.setBounds(400, 570, 100, 100);

        //---- btnDialect ----
        btnDialect.setIcon(new ImageIcon(getClass().getResource("/images/btn_henan.png")));
        btnDialect.setOpaque(false);
        btnDialect.setBorderPainted(false);
        btnDialect.setBackground(new Color(153, 153, 153));
        btnDialect.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                btnDialectMouseClicked(e);
            }
        });
        add(btnDialect);
        btnDialect.setBounds(550, 570, 100, 100);

        setPreferredSize(new Dimension(900, 765));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JLabel label1;
    private JTextField txtFile;
    private JButton btnChooser;
    private JScrollPane scrollPane1;
    private JTextArea areaText;
    private JLabel label2;
    private JLabel label3;
    private JSlider sldSpeed;
    private JSlider sldVolume;
    private JButton btnMale;
    private JButton btnFemale;
    private JButton btnDialect;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
